package net.bcxuexi.socket.stream.inhandle;

import net.bcxuexi.config.Config;
import net.bcxuexi.socket.stream.ReadStream;
import net.bcxuexi.socket.stream.StreamData;
import net.bcxuexi.tools.MyLog;

/**
 * 输入流数据队列处理器。
 * 读到的数据放到队列，一个一个处理
 */
public class ReadStreamHandler extends AbstractInputStreamHandler{
	private ReadStream readStream;
	/**
	 * 内网连接的输入消息处理器
	 */
	private InnerInputStreamHandler innerInputStreamHandler;
	/**
	 * 处理客户端与服务器数据连接输入流数据
	 */
	private DataInputStreamHandler dataInputStreamHandler;
	/**
	 * 控制连接输入流数据处理
	 */
	private ControlInputStreamHandler controlInputStreamHandler;
	public ReadStreamHandler(ReadStream readStream){
		this.readStream = readStream;
	}
	@Override
	public void handle() {
		StreamData streamData = null;// 待处理数据

		if (streamData == null) {
			try {
				streamData = take();// 从阻塞队列中取数据，可能会被阻塞
			} catch (InterruptedException e) {
				// 没有取到数据
				return;
			}
		}

		if (streamData == null) {
			MyLog.info("ReadStreamHandler队列为空，休息！");
			return;
		}
		// 数据处理
		process(streamData);
		try {
			sleep(80);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	private void process(StreamData streamData) {
		String socketType = streamData.getSocketModel().getType();
		byte buffer[]=streamData.data;
		int total = streamData.total;
		if (Config.SocketType.inner.equals(socketType)) {// 外部socket，肯定是数据通信
			if(Config.debug){
				String msg = new String(buffer,0,total);
				MyLog.info("接收到代理数据："+msg);
			}
			// 代理连接输入数据处理
			if(innerInputStreamHandler==null){
				innerInputStreamHandler = new InnerInputStreamHandler();
				innerInputStreamHandler.start();
			}
			try {
				innerInputStreamHandler.addData(streamData);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		} else {// 内网穿透socket，第0字节代表消息类型
			byte msgType = buffer[0];
			if(Config.debug){
				String msg = new String(buffer,0,total);
				MyLog.info("接收到服务器数据 type="+msgType);
				MyLog.info("接收到服务器数据："+msg);
			}
			if (msgType==1) {// 控制型消息
				try {
					if(controlInputStreamHandler==null){
						controlInputStreamHandler = new ControlInputStreamHandler();
						controlInputStreamHandler.start();
					}
					controlInputStreamHandler.addData(streamData);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			} else if (msgType==2) {// 数据型消息
				if(dataInputStreamHandler==null){
					dataInputStreamHandler = new DataInputStreamHandler();
					dataInputStreamHandler.start();
				}
				try {
					dataInputStreamHandler.addData(streamData);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			} else {
				// 未知输入数据，断开socket
				streamData.getSocketModel().closeSocket("未知消息类型,关闭socket服务."
						+ streamData.getSocketModel().toString());
				return;
			}
		}
	}
	/**
	 * 立即停止处理。会清空所有的任务，在执行的任务不会被停止
	 */
	public void stopHandleNow() {
		super.stopHandleNow();
		if(innerInputStreamHandler!=null){
			innerInputStreamHandler.stopHandleNow();
		}
		if(dataInputStreamHandler!=null){
			dataInputStreamHandler.stopHandleNow();
		}
		if(controlInputStreamHandler!=null){
			controlInputStreamHandler.stopHandleNow();
		}
	}

	/**
	 * 当任务队列为空停止调度。
	 */
	public void stopHandler() {
		super.stopHandler();
		if(innerInputStreamHandler!=null){
			innerInputStreamHandler.stopHandler();
		}
		if(dataInputStreamHandler!=null){
			dataInputStreamHandler.stopHandler();
		}
		if(controlInputStreamHandler!=null){
			controlInputStreamHandler.stopHandler();
		}
	}
}
